package com.vlong.town.util.config.path;

import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.interceptor.PromiseInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.MultipartConfigElement;

/**
 * Tomcat虚拟路径配置
 * @author Assassin
 */
@Configuration
public class VirtualPathConfig implements WebMvcConfigurer {

    @Autowired
    private PromiseInterceptor promiseInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        /**
         * 用SpringBoot内置的tomcat时有效，用外部的tomcat也有效
         * 所以用到外部的tomcat时不需在tomcat/config下的相应文件配置虚拟路径了,阿里云linux也没问题
         */

        // Windows配置
        // registry.addResourceHandler("/upload/image/**").addResourceLocations("file:G:/upload/image/");
        // registry.addResourceHandler("/upload/video/**").addResourceLocations("file:G:/upload/video/");

        // Linux配置
        // 阿里云(映射路径去除盘符)
        //registry.addResourceHandler("/upload/image/**").addResourceLocations("/upload/image/");
        //registry.addResourceHandler("/upload/video/**").addResourceLocations("/upload/video/");

         registry.addResourceHandler("/cdkeditor/**").addResourceLocations("file:" + TownConstant.CDKEDITOR_IMAGE_PATH);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(promiseInterceptor);
    }
    
    @Bean  
    public MultipartConfigElement multipartConfigElement() {  
        MultipartConfigFactory factory = new MultipartConfigFactory();  
        //单个文件最大  
        factory.setMaxFileSize("10240KB"); //KB,MB  
        /// 设置总上传数据总大小  
        factory.setMaxRequestSize("102400KB");  
        return factory.createMultipartConfig();  
    }  
}
